<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

set_time_limit(600);

class JFX_Module_Content extends JFX_Module{

    public $keyname = 'content';
    public $title = 'JFX Content';
    public $ajaxEnabled = true;
    public $version = '0.2.3';
    public $defaultContent = 'Error-Success Messages go here';
    

    public function __construct(){
        $args = func_get_args();
        eval("parent::__construct('".implode("', '", $args)."');");

        $CONFIG = JFX::registry('config');
        $VIEW = JFX::registry('JFX_View');




        //$this->addStyleSheet('default-layouts.css');
        //$VIEW->addIeCssLink($CONFIG->baseUrl.$CONFIG->moduleCssFolder.'/'.$this->keyname.'/ie6-hacks.css');

    }


    public function getBlockButtons($blockid){

        switch($blockDetails['module_key']){
            case 'content':
                switch($blockDetails['module_action']){
                    case 'content':
                        $button1 = new JFX_Block_Button;
                        $button1->alternativeText = 'Edit Content';
                        $button1->moduleName = 'content';
                        $button1->moduleMethod = 'updateSettings';
                        $button1->imageUrl = $this->imageUrl.'/edit.gif';
                        $button1->title = 'Edit block content';
                        $button1->blockId = $blockid;
                        return array($button1);
                    break;
                }
            break;
        }
    }


    public function getBlockActions(){
        return array(
            array(
                'keyname' => 'content',
                'title' => 'Wysiwyg Content',
                'description' => ''
            ),
            array(
                'keyname' => 'errorSuccess',
                'title' => 'Error / Success Messages',
                'description' => ''
            )
        );
    }

  

    public function delete($details){
        $this->deleteContentLang('heading', $details['id'], '*', $details['workspace_id']);
        $this->deleteContentLang('content', $details['id'], '*', $details['workspace_id']);
    }

    public function prepare($details){
        //$this->resetLanguages();
        return $this->getContent($details);
    }

    public function useCache($details){
        return false;
    }

    public function updateSettings($block){

        if(false === ($block instanceof JFX_Block)) return;
        $params = $block->getDetails();

        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        
        $id = $params['id'];

        switch($params['module_action']){
            case 'content':


                if(Antz_IntelliForm::submitted($this->keyname.'-edit-settings')){
                    // update our settings

                    $error = false;

                    if(!$error){

                        //$DB->showErrors(true);
                        //$DB->debug(true);
                        $this->updateContentLang('heading', $id, post('heading', '', 500));
                        $this->updateContentLang('content', $id, post('content', '', 500000));
                        //$DB->showErrors(false);
                        //$DB->debug(false);
                        JFX::addSuccess('Settings applied!');

                    }




                }

                $heading = $this->getContentLang('heading', $id);
                $content = $this->getContentLang('content', $id);

                //if($heading == '') $heading = $this->lang('default_heading');
                //if($content == '') $content = $this->lang('default_content');

                $formFields = array(
                    array(
                        'name' => 'heading',
                        'type' => 'text',
                        'value' => post('heading', $heading, 500),
                        'label' => $this->lang('edit_heading')
                    ),
                    array(
                        'name' => 'content',
                        'type' => 'be_tinymce',
                        'value' => post('content', $content, 500000),
                        'id' => $this->keyname.'-edit-content',
                        'label' => $this->lang('edit_content')
                    ),
                    array(
                        'name' => 'submitBtn',
                        'type' => 'submit',
                        'value' => $this->lang('edit_submit'),
                        'label' => '&nbsp;'
                    )
                );


                echo JFX::makeRapidForm($formFields, $this->lang('edit_legend'), $this->keyname.'-edit-settings');

            break;
            case 'errorSuccess':
                echo 'No actions defined for error/success messages';
            break;

        }


    }



    public function getContent($details){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        $SMARTY = JFX::registry('Smarty');

        if(is_object($details)){
            $details = JFX::objectToArray($details);
        }

        switch($details['module_action']){

            case 'content':

                $heading = $this->getContentLang('heading', $details['id'], '', $details['workspace_id']);
                $content = $this->getContentLang('content', $details['id'], '', $details['workspace_id']);

                $result = '';
                if(strlen(trim((string) $heading)) > 0) $result = '<h2 class="'.$this->keyname.'-heading">'.$heading.'</h2>
';
                if(strlen(trim((string) $content)) > 0) $result .= '<div class="'.$this->keyname.'-content">'.$content.'</div>
';
                eval($details['php_code']);
                $result = JFX::fetchSmarty($result);
                return $result;

            break;
            case 'errorSuccess':
                $retVal = JFX::getError().JFX::getSuccess();
                if(trim($retVal)=='') return $this->defaultContent;
                else return $retVal;
            break;

        }

    }




    public function getDefaultContent($action){
        switch($action){
            case 'content':
                $title = $this->lang('default_heading');
                $content = $this->lang('default_content');
                return '<h2>'.$title.'</h2>'.$content;
            break;
            case 'errorSuccess':
                return $this->defaultContent;
            break;
        }
    }




    public function copy($action, $oldBlockid, $newBlockid, $oldPageid, $newPageid, $oldWorkspaceid, $newWorkspaceid){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');

        $installedLangs = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}languages");
        foreach($installedLangs as $k=>$v){
            $heading = $this->getContentLang('heading', $oldBlockid, $v['keyname'], $oldWorkspaceid);
            $content = $this->getContentLang('content', $oldBlockid, $v['keyname'], $oldWorkspaceid);
            $this->updateContentLang('heading', $newBlockid, $heading, '*', $newWorkspaceid);
            $this->updateContentLang('content', $newBlockid, $content, '*', $newWorkspaceid);
        }

    }

    public function copyToNewBlock($oldid, $newid, $oldworkspaceid, $newworkspaceid){
        $DB = JFX::registry('db');
        //$DB->showOutput(true);
        //$DB->showErrors(true);
        $heading = $this->getContentLang('heading', $oldid, '', $oldworkspaceid);
        $content = $this->getContentLang('content', $oldid, '', $oldworkspaceid);

        $this->updateContentLang('heading', $newid, $heading, '*', $newworkspaceid);
        $this->updateContentLang('content', $newid, $content, '*', $newworkspaceid);
        //$DB->showOutput(false);
        //$DB->showErrors(false);
    }

    public function createDefaultBlock($action, $blockid, $workspaceid){

        switch($action){
            case 'content':
                $heading = $this->lang('default_heading');
                $content = $this->lang('default_content');
                $this->updateContentLang('heading', $blockid, $heading, '*', $workspaceid);
                $this->updateContentLang('content', $blockid, $content, '*', $workspaceid);
            break;
        };
    }

    public function install(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        require('install/install.php');
    }

    public function uninstall(){
        $DB = JFX::registry('db');
        $CONFIG = JFX::registry('config');
        require('install/uninstall.php');
    }
}